(function (root) {
    var rejectReg = ( /^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i );
    function jQuery(selector, context) {
        return new jQuery.prototype.init(selector, context)
    }
    jQuery.prototype = {
        constructor:jQuery,
        version:'0.0.2',
        length:0,
        selector:'',
        context:null,
        init: function (selector, context) {
            context = context || document
            /**
             *  1. selector 是否為字符串 ？ 
             *     為字符串則存在兩種可能 1. 創建元素 2. 查找元素
             *     不為字符串 則可能是 $(document) 、$(window)、$(function(){})
             * 
             */
            var match , elems;

            // $() $(underfine) $(null) $(false)
            if (!selector) {
                return null
            }
            if (jQuery.isString(selector)) {
                
                console.log(selector)

                if(selector[0]==='<' && selector[selector.length-1]==='>' && selector.length >= 3){
                    match = [selector]
                  
                
                }
                    // <a> 創建元素
                if(match){
                   jQuery.merge(this , jQuery.parseHtml( selector , context ))
                }else{
                    //  查找元素
                    elem = context.querySelectorAll(selector);
                    var elems = Array.prototype.slice.call(elem)
                    var index= 0;
                    this.length = elems.length

                    for (; index < elems.length; index++) {
                        this[index] = elems[index]
                    }
                    this.context = context
                    this.selector = selector
                }

            } else if (selector.nodeType) {

            } else if (jQuery.isFunction(selector)) {

            }
         
        }
    }
    jQuery.fn = jQuery.prototype

    jQuery.fn.init.prototype = jQuery.fn;

    jQuery.extend = jQuery.fn.extend = function () {

        var target = arguments[0];
        var length = arguments.length;
        var deep = false
        var i = 1;

        if (Object.prototype.toString.call(target).slice(8, -1) === 'Array') {
            deep = true;
            target = arguments[1]
            i++
        }

        if (typeof target != 'object') {
            target = {}
        }

        if (i === length) {
            target = this;
            i--
        }

        for (; i < length; i++) {
            var option = arguments[i];
            for (var name in option) {
                var copy = option[name];
                var src = target[name];
                var isArray = Object.prototype.toString.call(copy).slice(8, -1) === 'Array';
                var isObject = Object.prototype.toString.call(copy).slice(8, -1) === 'Object'
                var clone = {};
                if (deep && (isArray || isObject)) {
                    if (isArray) {
                        clone = Object.prototype.toString.call(src).slice(8, -1) === 'Array' ? src : [];
                    } else if (isObject) {
                        clone = Object.prototype.toString.call(src).slice(8, -1) === 'Object' ? src : {}
                    } else {
                        clone = src
                    }
                    target[name] = jQuery.extend(deep, clone, copy)
                } else if (copy !== undefined) {
                    target[name] = copy
                }
            }
        }

        return target
    }

    jQuery.extend({
        isPlainObject: function (data) {
            return Object.prototype.toString.call(data).slice(8, -1) === 'Object'
        },
        isFunction: function (fn) {
            return Object.prototype.toString.call(fn).slice(8, -1) === 'Function'
        },
        isArray: function (data) {
            return Object.prototype.toString.call(data).slice(8, -1) === 'Array'
        },
        isBoolean: function (bool) {
            return typeof bool === 'boolean'
        },
        isString: function (str) {
            return typeof str === 'string'
        },
        parseHtml:function (str , context) {
            if(!str){
                return null
            }
            var parsed = rejectReg.exec(str)
            if(parsed){
                return [ context.createElement(parsed[1]) ]
            }
        },
        //  合併數組
        merge:function (first , second) {
            var l = first.length;
            var s = second.length;
            var i = 0;

            if( typeof l === 'number' ){
              
                for (; i < s; i++) {
                    first[l++] = second[i]
                }
                first.length = l
            }

           

        }
    })

    //  将jQuery挂载到window（全局变量）上
    root.jQuery = root.$ = jQuery
})(this)